// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package qunar.tc.decompiler.main;

import qunar.tc.decompiler.code.CodeConstants;
import qunar.tc.decompiler.main.ClassesProcessor.ClassNode;
import qunar.tc.decompiler.main.extern.IFernflowerPreferences;
import qunar.tc.decompiler.main.rels.ClassWrapper;
import qunar.tc.decompiler.main.rels.MethodWrapper;
import qunar.tc.decompiler.modules.decompiler.exps.*;
import qunar.tc.decompiler.modules.decompiler.stats.RootStatement;
import qunar.tc.decompiler.modules.decompiler.stats.Statement;
import qunar.tc.decompiler.modules.decompiler.stats.Statements;
import qunar.tc.decompiler.modules.decompiler.vars.VarVersionPair;
import qunar.tc.decompiler.struct.StructClass;
import qunar.tc.decompiler.struct.StructField;
import qunar.tc.decompiler.util.InterpreterUtil;

import java.util.ArrayList;
import java.util.List;

public class InitializerProcessor {
    public static void extractInitializers(ClassWrapper wrapper) {
        MethodWrapper method = wrapper.getMethodWrapper(CodeConstants.CLINIT_NAME, "()V");
        if (method != null && method.root != null) {  // successfully decompiled static constructor
            extractStaticInitializers(wrapper, method);
        }

        extractDynamicInitializers(wrapper);

        // required e.g. if anonymous class is being decompiled as a standard one.
        // This can happen if InnerClasses attributes are erased
        liftConstructor(wrapper);

        if (DecompilerContext.getOption(IFernflowerPreferences.HIDE_EMPTY_SUPER)) {
            hideEmptySuper(wrapper);
        }
    }

    private static void liftConstructor(ClassWrapper wrapper) {
        for (MethodWrapper method : wrapper.getMethods()) {
            if (CodeConstants.INIT_NAME.equals(method.methodStruct.getName()) && method.root != null) {
                Statement firstData = Statements.findFirstData(method.root);
                if (firstData == null) {
                    return;
                }

                int index = 0;
                List<Exprent> lstExprents = firstData.getExprents();

                for (Exprent exprent : lstExprents) {
                    int action = 0;

                    if (exprent.type == Exprent.EXPRENT_ASSIGNMENT) {
                        AssignmentExprent assignExpr = (AssignmentExprent) exprent;
                        if (assignExpr.getLeft().type == Exprent.EXPRENT_FIELD && assignExpr.getRight().type == Exprent.EXPRENT_VAR) {
                            FieldExprent fExpr = (FieldExprent) assignExpr.getLeft();
                            if (fExpr.getClassname().equals(wrapper.getClassStruct().qualifiedName)) {
                                StructField structField = wrapper.getClassStruct().getField(fExpr.getName(), fExpr.getDescriptor().descriptorString);
                                if (structField != null && structField.hasModifier(CodeConstants.ACC_FINAL)) {
                                    action = 1;
                                }
                            }
                        }
                    } else if (index > 0 && exprent.type == Exprent.EXPRENT_INVOCATION &&
                            Statements.isInvocationInitConstructor((InvocationExprent) exprent, method, wrapper, true)) {
                        // this() or super()
                        lstExprents.add(0, lstExprents.remove(index));
                        action = 2;
                    }

                    if (action != 1) {
                        break;
                    }

                    index++;
                }
            }
        }
    }

    private static void hideEmptySuper(ClassWrapper wrapper) {
        for (MethodWrapper method : wrapper.getMethods()) {
            if (CodeConstants.INIT_NAME.equals(method.methodStruct.getName()) && method.root != null) {
                Statement firstData = Statements.findFirstData(method.root);
                if (firstData == null || firstData.getExprents().isEmpty()) {
                    return;
                }

                Exprent exprent = firstData.getExprents().get(0);
                if (exprent.type == Exprent.EXPRENT_INVOCATION) {
                    InvocationExprent invExpr = (InvocationExprent) exprent;
                    if (Statements.isInvocationInitConstructor(invExpr, method, wrapper, false) && invExpr.getLstParameters().isEmpty()) {
                        firstData.getExprents().remove(0);
                    }
                }
            }
        }
    }

    private static void extractStaticInitializers(ClassWrapper wrapper, MethodWrapper method) {
        RootStatement root = method.root;
        StructClass cl = wrapper.getClassStruct();
        Statement firstData = Statements.findFirstData(root);
        if (firstData != null) {
            boolean inlineInitializers = cl.hasModifier(CodeConstants.ACC_INTERFACE) || cl.hasModifier(CodeConstants.ACC_ENUM);

            while (!firstData.getExprents().isEmpty()) {
                Exprent exprent = firstData.getExprents().get(0);

                boolean found = false;

                if (exprent.type == Exprent.EXPRENT_ASSIGNMENT) {
                    AssignmentExprent assignExpr = (AssignmentExprent) exprent;
                    if (assignExpr.getLeft().type == Exprent.EXPRENT_FIELD) {
                        FieldExprent fExpr = (FieldExprent) assignExpr.getLeft();
                        if (fExpr.isStatic() && fExpr.getClassname().equals(cl.qualifiedName) &&
                                cl.hasField(fExpr.getName(), fExpr.getDescriptor().descriptorString)) {

                            // interfaces fields should always be initialized inline
                            if (inlineInitializers || isExprentIndependent(assignExpr.getRight(), method)) {
                                String keyField = InterpreterUtil.makeUniqueKey(fExpr.getName(), fExpr.getDescriptor().descriptorString);
                                if (!wrapper.getStaticFieldInitializers().containsKey(keyField)) {
                                    wrapper.getStaticFieldInitializers().addWithKey(assignExpr.getRight(), keyField);
                                    firstData.getExprents().remove(0);
                                    found = true;
                                }
                            }
                        }
                    }
                }

                if (!found) {
                    break;
                }
            }
        }
    }

    private static void extractDynamicInitializers(ClassWrapper wrapper) {
        StructClass cl = wrapper.getClassStruct();

        boolean isAnonymous = DecompilerContext.getClassProcessor().getMapRootClasses().get(cl.qualifiedName).type == ClassNode.CLASS_ANONYMOUS;

        List<List<Exprent>> lstFirst = new ArrayList<>();
        List<MethodWrapper> lstMethodWrappers = new ArrayList<>();

        for (MethodWrapper method : wrapper.getMethods()) {
            if (CodeConstants.INIT_NAME.equals(method.methodStruct.getName()) && method.root != null) { // successfully decompiled constructor
                Statement firstData = Statements.findFirstData(method.root);
                if (firstData == null || firstData.getExprents().isEmpty()) {
                    return;
                }
                lstFirst.add(firstData.getExprents());
                lstMethodWrappers.add(method);

                Exprent exprent = firstData.getExprents().get(0);
                if (!isAnonymous) { // FIXME: doesn't make sense
                    if (exprent.type != Exprent.EXPRENT_INVOCATION ||
                            !Statements.isInvocationInitConstructor((InvocationExprent) exprent, method, wrapper, false)) {
                        return;
                    }
                }
            }
        }

        if (lstFirst.isEmpty()) {
            return;
        }

        while (true) {
            String fieldWithDescr = null;
            Exprent value = null;

            for (int i = 0; i < lstFirst.size(); i++) {
                List<Exprent> lst = lstFirst.get(i);

                if (lst.size() < (isAnonymous ? 1 : 2)) {
                    return;
                }

                Exprent exprent = lst.get(isAnonymous ? 0 : 1);

                boolean found = false;

                if (exprent.type == Exprent.EXPRENT_ASSIGNMENT) {
                    AssignmentExprent assignExpr = (AssignmentExprent) exprent;
                    if (assignExpr.getLeft().type == Exprent.EXPRENT_FIELD) {
                        FieldExprent fExpr = (FieldExprent) assignExpr.getLeft();
                        if (!fExpr.isStatic() && fExpr.getClassname().equals(cl.qualifiedName) &&
                                cl.hasField(fExpr.getName(), fExpr.getDescriptor().descriptorString)) { // check for the physical existence of the field. Could be defined in a superclass.

                            if (isExprentIndependent(assignExpr.getRight(), lstMethodWrappers.get(i))) {
                                String fieldKey = InterpreterUtil.makeUniqueKey(fExpr.getName(), fExpr.getDescriptor().descriptorString);
                                if (fieldWithDescr == null) {
                                    fieldWithDescr = fieldKey;
                                    value = assignExpr.getRight();
                                } else {
                                    if (!fieldWithDescr.equals(fieldKey) ||
                                            !value.equals(assignExpr.getRight())) {
                                        return;
                                    }
                                }
                                found = true;
                            }
                        }
                    }
                }

                if (!found) {
                    return;
                }
            }

            if (!wrapper.getDynamicFieldInitializers().containsKey(fieldWithDescr)) {
                wrapper.getDynamicFieldInitializers().addWithKey(value, fieldWithDescr);

                for (List<Exprent> lst : lstFirst) {
                    lst.remove(isAnonymous ? 0 : 1);
                }
            } else {
                return;
            }
        }
    }

    private static boolean isExprentIndependent(Exprent exprent, MethodWrapper method) {
        List<Exprent> lst = exprent.getAllExprents(true);
        lst.add(exprent);

        for (Exprent expr : lst) {
            switch (expr.type) {
                case Exprent.EXPRENT_VAR:
                    VarVersionPair varPair = new VarVersionPair((VarExprent) expr);
                    if (!method.varproc.getExternalVars().contains(varPair)) {
                        String varName = method.varproc.getVarName(varPair);
                        if (!varName.equals("this") && !varName.endsWith(".this")) { // FIXME: remove direct comparison with strings
                            return false;
                        }
                    }
                    break;
                case Exprent.EXPRENT_FIELD:
                    return false;
            }
        }

        return true;
    }
}